import axios, { AxiosResponse } from '@ohos/axios';
import instance from '../utils/request';

// 定义平台类型
export type Platform = 'kuaishouhao' | 'xiaohongshu' | 'douyin';

// 定义内容类型
export type ContentType = 'news' | 'video';

// 定义权限类型
export type Permission = 0 | 1;

// 定义快手发布参数接口
export interface KuaishouParams {
  video: string;
  cover: string;
  summary: string;
  talk?: string[];
  at_users?: string[];
}

// 定义小红书发布图文参数接口
export interface XiaohongshuNewsParams {
  title: string;
  content_type: 'news';
  images: string[];
  summary: string;
  talk?: string[];
  permission: Permission;
  author_statement: boolean;
}

// 定义小红书发布视频参数接口
export interface XiaohongshuVideoParams {
  title: string;
  content_type: 'video';
  video: string;
  cover: string;
  summary: string;
  talk?: string[];
  permission: Permission;
  author_statement: boolean;
}

// 定义抖音发布视频参数接口
export interface DouyinVideoParams {
  title: string;
  video: string;
  cover: string;
  talk?: string[];
  summary: string;
  post_id: string;
}

// 定义统一发布参数接口
export interface PublishParams {
  platform: Platform;
  // 快手参数
  kuaishou?: KuaishouParams;
  // 小红书参数
  xiaohongshu?: XiaohongshuNewsParams | XiaohongshuVideoParams;
  // 抖音参数
  douyin?: DouyinVideoParams;
}

// 定义响应数据结构接口
export interface PublishResponse {
  success: boolean;
  code: number;
  message: string;
  data?: {
    video_id?: string;
    post_id?: string;
    publish_time?: string;
  };
}

// 统一发布接口 - 根据平台自动构建对应的请求体
export const publish = (
  params: PublishParams
): Promise<AxiosResponse<PublishResponse>> => {
  const { platform } = params;

  let requestData: any = {
    command: '',
    platform: '',
    params: {},
  };

  switch (platform) {
    case 'kuaishouhao':
      if (!params.kuaishou) {
        throw new Error('快手平台参数不能为空');
      }
      requestData = {
        command: 'publish',
        platform: 'kuaishouhao',
        params: {
          video: params.kuaishou.video,
          cover: params.kuaishou.cover,
          summary: params.kuaishou.summary,
          talk: params.kuaishou.talk || [],
          at_users: params.kuaishou.at_users || [],
        },
      };
      break;

    case 'xiaohongshu':
      if (!params.xiaohongshu) {
        throw new Error('小红书平台参数不能为空');
      }
      requestData = {
        command: 'publish',
        platform: 'xiaohongshu',
        params: {
          title: params.xiaohongshu.title,
          content_type: params.xiaohongshu.content_type,
          summary: params.xiaohongshu.summary,
          talk: params.xiaohongshu.talk || [],
          permission: 1,
          author_statement: true,
        },
      };

      // 根据内容类型添加对应字段
      if (params.xiaohongshu.content_type === 'news') {
        requestData.params.images = (
          params.xiaohongshu as XiaohongshuNewsParams
        ).images;
      } else {
        requestData.params.video = (
          params.xiaohongshu as XiaohongshuVideoParams
        ).video;
        requestData.params.cover = (
          params.xiaohongshu as XiaohongshuVideoParams
        ).cover;
      }
      break;

    case 'douyin':
      if (!params.douyin) {
        throw new Error('抖音平台参数不能为空');
      }
      requestData = {
        command: 'postVideo',
        platform: 'douyin',
        params: {
          title: params.douyin.title,
          video: params.douyin.video,
          cover: params.douyin.cover,
          talk: params.douyin.talk || [],
          summary: params.douyin.summary,
          post_id: params.douyin.post_id,
        },
      };
      break;

    default:
      throw new Error(`不支持的平台: ${platform}`);
  }

  return instance.post('/api/v1/command', requestData);
};

// 为了向后兼容，保留原有的单独接口（可选）
export const publishKuaishouVideo = (
  params: KuaishouParams
): Promise<AxiosResponse<PublishResponse>> => {
  return publish({
    platform: 'kuaishouhao',
    kuaishou: params,
  });
};

export const publishXiaohongshuNews = (
  params: XiaohongshuNewsParams
): Promise<AxiosResponse<PublishResponse>> => {
  return publish({
    platform: 'xiaohongshu',
    xiaohongshu: params,
  });
};

export const publishXiaohongshuVideo = (
  params: XiaohongshuVideoParams
): Promise<AxiosResponse<PublishResponse>> => {
  return publish({
    platform: 'xiaohongshu',
    xiaohongshu: params,
  });
};

export const publishDouyinVideo = (
  params: DouyinVideoParams
): Promise<AxiosResponse<PublishResponse>> => {
  return publish({
    platform: 'douyin',
    douyin: params,
  });
};

// /*********************** 使用示例 ***********************/
// // 快手发布示例
// publish({
//   platform: 'kuaishouhao',
//   kuaishou: {
//     video: "http://sm75d8jmh.hn-bkt.clouddn.com/videos/douyin/1730702799281_yt70qj.mp4",
//     cover: "http://sm75d8jmh.hn-bkt.clouddn.com/uploads/image/1730376997249-su6yh8.png",
//     summary: "da大师大师大多少啊多少啊的撒的撒多少啊多少啊ddsadasdasd"
//   }
// })
//   .then((response: AxiosResponse) => {
//     console.log('发布成功:', response.data);
//   })
//   .catch((error: any) => {
//     console.error('发布失败:', error);
//   });

// // 小红书发布图文示例
// publish({
//   platform: 'xiaohongshu',
//   xiaohongshu: {
//     title: "的撒的撒的多少啊多少啊多少啊多少啊多少啊",
//     content_type: "news",
//     images: [
//       "http://sm75d8jmh.hn-bkt.clouddn.com/uploads/image/1730376997249-su6yh8.png",
//       "http://sm75d8jmh.hn-bkt.clouddn.com/uploads/images/1730854543948-ex8bws.png"
//     ],
//     summary: "的撒的撒大",
//     talk: ["风景", "人文"],
//     permission: 1,
//     author_statement: true
//   }
// })
//   .then((response: AxiosResponse) => {
//     console.log('小红书图文发布成功:', response.data);
//   })
//   .catch((error: any) => {
//     console.error('小红书图文发布失败:', error);
//   });

// // 小红书发布视频示例
// publish({
//   platform: 'xiaohongshu',
//   xiaohongshu: {
//     title: "的撒打算打算的撒的撒大师多少啊",
//     content_type: "video",
//     video: "http://sm75d8jmh.hn-bkt.clouddn.com/uploads/video/1730460386413-hmc2rk.mp4",
//     cover: "http://sm75d8jmh.hn-bkt.clouddn.com/uploads/image/1730442395215-1dbm9d.jpeg",
//     summary: "dsadsad dsa dsa dsad sadsa",
//     talk: ["风景", "人文"],
//     permission: 1,
//     author_statement: true
//   }
// })
//   .then((response: AxiosResponse) => {
//     console.log('小红书视频发布成功:', response.data);
//   })
//   .catch((error: any) => {
//     console.error('小红书视频发布失败:', error);
//   });

// // 抖音发布视频示例
// publish({
//   platform: 'douyin',
//   douyin: {
//     title: "哈喽123",
//     video: "http://sm75d8jmh.hn-bkt.clouddn.com/uploads/video/1730376986502-ccy69v.mp4",
//     cover: "http://sm75d8jmh.hn-bkt.clouddn.com/uploads/image/1730442395215-1dbm9d.jpeg",
//     talk: ["#生活记录", "#春天来了", "#樱花季", "#Vlog日常"],
//     summary: "的撒的撒春天来了，樱花开了，记录下这美好的一刻。阳光正好，微风不燥，这样的日子真想一直延续下去。",
//     post_id: "post_123456"
//   }
// })
//   .then((response: AxiosResponse) => {
//     console.log('抖音视频发布成功:', response.data);
//   })
//   .catch((error: any) => {
//     console.error('抖音视频发布失败:', error);
//   });
